package com.lge.clock.alarmclock;
   
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.lge.clock.util.CommonUtil;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
/**
 * alarm provider (alarm, snooze, city) table 
 * @author donggeun.kim
 *
 */ 
public class ALProvider extends ContentProvider {
	private static final String TAG = "ALProvider";

	private static final String DATABASE_NAME = "alarms.db";
	private static final String ALARM_TABLE_NAME = "alarms";
	
	private static final String URI_AUTHORITY = "com.lge.clock.alarmclock.ALProvider";
	
	private static final int ALARMS   = 1;
	private static final int ALARM_ID = 2;
	 
	private static final int CALCUS   = 3;
	private static final int CALCU_ID = 4;
	
	private static final int CITIES   = 5;
	private static final int CITY_ID  = 6;
	
	private static final UriMatcher MATCHER;
	private static Map<String, String> sAlarmsListProjection;
	private static Map<String, String> sCalcusListProjection;
//	private static Map<String, String> CITIES_LIST_PROJECTION;
	
	private static final int DEF_STR_BUF_SQL_SIZE = 128 * 5; 
	
	private SQLiteDatabase db;
	//[mo2sangbong.lee][1455] 2011.10.18 START
	public static SQLiteOpenHelper mOpenHelper;
	//[mo2sangbong.lee][1455] 2011.10.18 END                       
		
	public static final String CONTENT_ALARMS_URI_STRING = "content://"+URI_AUTHORITY+"/" + getAlarmTableName();
	
	/**
	 * Field CONTENT_ALARMS_URI.
	 */
	public static final Uri CONTENT_ALARMS_URI  = Uri.parse(CONTENT_ALARMS_URI_STRING);
	
	/**
	 * Field CONTENT_CALCU_URI.
	 */
	public static final Uri CONTENT_CALCU_URI   = Uri.parse("content://"+URI_AUTHORITY+"/calcus");
	
	
	static {
		MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
		MATCHER.addURI(URI_AUTHORITY, getAlarmTableName(),    ALARMS);
		MATCHER.addURI(URI_AUTHORITY, getAlarmTableName()+"/#",  ALARM_ID);
		
		MATCHER.addURI(URI_AUTHORITY, "calcus",    CALCUS);
		MATCHER.addURI(URI_AUTHORITY, "calcus/#",  CALCU_ID);
		//////////////////////////////////////////////////////////////
		sAlarmsListProjection=new HashMap<String, String>();
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_ID,	AlarmMeta.COLUMNS.ALARM_ID );
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_HOUR,	AlarmMeta.COLUMNS.ALARM_HOUR);
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_MINUTES,	AlarmMeta.COLUMNS.ALARM_MINUTES);
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_DAYSOFWEEK,	AlarmMeta.COLUMNS.ALARM_DAYSOFWEEK);
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_ENABLED,	AlarmMeta.COLUMNS.ALARM_ENABLED);
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_SNOOZE,	AlarmMeta.COLUMNS.ALARM_SNOOZE);
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_TONE,   AlarmMeta.COLUMNS.ALARM_TONE);
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_VIB,	AlarmMeta.COLUMNS.ALARM_VIB);
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_MEMO,	AlarmMeta.COLUMNS.ALARM_MEMO);
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_PUZZLE,	AlarmMeta.COLUMNS.ALARM_PUZZLE);		
		sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_WIDGET_YN,	AlarmMeta.COLUMNS.ALARM_WIDGET_YN);
		
/* [I3.0] Code START */
		if(CommonUtil.isUseDB2()){
			sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_VOLUME,	AlarmMeta.COLUMNS.ALARM_VOLUME);
			sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_LINK,	AlarmMeta.COLUMNS.ALARM_LINK);
			sAlarmsListProjection.put(AlarmMeta.COLUMNS.ALARM_LINK_TEXT,	AlarmMeta.COLUMNS.ALARM_LINK_TEXT);			
		}
/* [I3.0] Code END */
		
		////////////////////////////////////////////////////////////
		sCalcusListProjection=new HashMap<String, String>();
		sCalcusListProjection.put("_id",          "_id");
		sCalcusListProjection.put("aid",          "aid");
		sCalcusListProjection.put("aindex",       "aindex");
		
		sCalcusListProjection.put("hour",         "hour");
		sCalcusListProjection.put("minutes",      "minutes");
		sCalcusListProjection.put("daysofweek",   "daysofweek");
		sCalcusListProjection.put("enabled", 	   "enabled");
		sCalcusListProjection.put("time",    	   "time");
		sCalcusListProjection.put("status",       "status");   
		sCalcusListProjection.put("stop",         "stop");
	}               

	public static String getDBName() {
		return DATABASE_NAME;
	}
	
	public static String getAlarmTableName(){
		return ALARM_TABLE_NAME;
	}
	 
	public int getDbVersion() {
		return(CommonUtil.isUseDB2() ? 2 : 1);
	}
	//[mo2sangbong.lee][1455] 2011.10.18 START
	public static SQLiteOpenHelper getmOpenHelper() {
		return mOpenHelper;
	}
	//[mo2sangbong.lee][1455] 2011.10.18 END
	private class DatabaseHelper extends SQLiteOpenHelper {
		/**
		 * Constructor for DatabaseHelper.
		 * @param context Context
		 */
		private DatabaseHelper(Context context) {
			super(context, getDBName(), null, getDbVersion());
		}
		  
		/**
		 * Method onCreate.
		 * @param db SQLiteDatabase
		 */
		@Override 
		public void onCreate(SQLiteDatabase db) {
			StringBuffer creation = null;
			
			Cursor c=db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name=" + "'"+getAlarmTableName()+"'", null);
			
			try {
				if (c != null && c.getCount()==0) {//[hyunjung78.park] 2011.08.09 cursor null check

					int version = getDbVersion();
					createAlarmTable(db, version);
					insertAlarmDefault(db, version);
				
					Log.d(TAG, "CREATE TABLE Success "+ getAlarmTableName() + " ver = " + version);
				}
			}
			finally {
				if ( c != null )
					c.close();
			}
	        ///////////////////////////////////////////////////////////////////
			       
			c=db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='calcus'", null);
			try {        
				if (c != null && c.getCount()==0) {//[hyunjung78.park] 2011.08.09 cursor null check
					creation = new StringBuffer(DEF_STR_BUF_SQL_SIZE);
					creation.append("CREATE TABLE calcus (  _id        INTEGER  PRIMARY KEY, ");
					creation.append("					    aid	       INTEGER, 	         ");
					creation.append("					    aindex	   INTEGER, 	         ");
					
					creation.append("					    hour	   INTEGER, 	        ");
					creation.append("					    minutes	   INTEGER, 	        ");
					creation.append("                       daysofweek INTEGER, 	        ");
					creation.append("                       enabled    TEXT, 	            ");		
					
					creation.append("					    time	   INTEGER, 	         ");
					creation.append("					    status	   TEXT,          		 ");
					creation.append("					    stop	   TEXT       ); 		 ");
					db.execSQL(creation.toString());					
					db.execSQL("insert into calcus values (1,1,0,  9,0,0,'0',  0, 'A', '')  ");	 
					////////////////////////////////////////////////////////////////////////////////////////////
					Log.d(TAG, "CREATE TABLE calcus");
					Log.d(TAG, "insert into calcus values (1,1,0,  9,0,0,'0',  0, 'A', '')  ");
				}                          
			}      
			finally {
				if ( c != null )//[hyunjung78.park] 2011.08.09 cursor null check
					c.close();   
			}   
			/////////////////////////////////////////////////////////////////////////////////////////////
			
			
		}         
		
		private void createAlarmTable(SQLiteDatabase database, int version){
			StringBuffer creation = null;	
			creation = new StringBuffer(DEF_STR_BUF_SQL_SIZE);
			creation.append("CREATE TABLE "+getAlarmTableName() + " ( ");
			creation.append( AlarmMeta.COLUMNS.ALARM_ID 		+ " INTEGER PRIMARY KEY, ");
			creation.append( AlarmMeta.COLUMNS.ALARM_HOUR		+ " INTEGER, ");
			creation.append( AlarmMeta.COLUMNS.ALARM_MINUTES 	+ " INTEGER, ");
			creation.append( AlarmMeta.COLUMNS.ALARM_DAYSOFWEEK + " INTEGER, ");					
			creation.append( AlarmMeta.COLUMNS.ALARM_ENABLED 	+ " TEXT, ");		
			creation.append( AlarmMeta.COLUMNS.ALARM_SNOOZE		+ " INTEGER, ");
			creation.append( AlarmMeta.COLUMNS.ALARM_TONE		+ " TEXT, ");		
			creation.append( AlarmMeta.COLUMNS.ALARM_VIB		+ " TEXT, ");	
			creation.append( AlarmMeta.COLUMNS.ALARM_PUZZLE		+ " TEXT, ");
			creation.append( AlarmMeta.COLUMNS.ALARM_MEMO 		+ " TEXT, ");
			creation.append( AlarmMeta.COLUMNS.ALARM_WIDGET_YN 	+ " TEXT ");		
			
			if(version == 2){	
				creation.append(" , ");
				creation.append( AlarmMeta.COLUMNS.ALARM_VOLUME + " TEXT, ");
				creation.append( AlarmMeta.COLUMNS.ALARM_LINK 	+ " TEXT, ");
				creation.append( AlarmMeta.COLUMNS.ALARM_LINK_TEXT 	+ " TEXT ");				
			}
			
			creation.append(" ); ");
			
			Log.d(TAG, "execSQL : " + creation.toString());
			
			database.execSQL(creation.toString());
		}
		
		private void insertAlarmDefault(SQLiteDatabase database,int version){
			StringBuffer query = new StringBuffer("insert into");
			query.append(" " + getAlarmTableName());
			query.append("  values (1, 9, 0, 0, '0', 5, 'On', 'On', 'Off', '', 'N'");
			
			if(version == 2){
				query.append(", '-1', 'None', '' ");
			}
			
			query.append(" ); ");
			Log.d(TAG, query.toString());
			
			database.execSQL(query.toString());
		}
		
		private void insertAlarm(SQLiteDatabase database, int oldVersion, int newVersion, String oldTable){
			String COMMA = " , ";
			StringBuffer query = new StringBuffer("INSERT INTO ");
			query.append(" " + getAlarmTableName() + " ( ");
			query.append( AlarmMeta.COLUMNS.ALARM_ID 			+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_HOUR			+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_MINUTES 		+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_DAYSOFWEEK 	+ COMMA);					
			query.append( AlarmMeta.COLUMNS.ALARM_ENABLED 		+ COMMA);		
			query.append( AlarmMeta.COLUMNS.ALARM_SNOOZE		+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_TONE			+ COMMA);		
			query.append( AlarmMeta.COLUMNS.ALARM_VIB			+ COMMA);	
			query.append( AlarmMeta.COLUMNS.ALARM_PUZZLE		+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_MEMO 			+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_WIDGET_YN 	);
			query.append( " ) ");
			query.append( "SELECT ");
			query.append( AlarmMeta.COLUMNS.ALARM_ID 			+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_HOUR			+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_MINUTES 		+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_DAYSOFWEEK 	+ COMMA);					
			query.append( AlarmMeta.COLUMNS.ALARM_ENABLED 		+ COMMA);		
			query.append( AlarmMeta.COLUMNS.ALARM_SNOOZE		+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_TONE			+ COMMA);		
			query.append( AlarmMeta.COLUMNS.ALARM_VIB			+ COMMA);	
			query.append( AlarmMeta.COLUMNS.ALARM_PUZZLE		+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_MEMO 			+ COMMA);
			query.append( AlarmMeta.COLUMNS.ALARM_WIDGET_YN 	);
			query.append( " FROM " + oldTable);

			Log.d(TAG, query.toString());			
			database.execSQL(query.toString());
		}
		
		private void onUpgradeAlarmTable(SQLiteDatabase database, int oldVersion,
				int newVersion){
		// *************************
		// ALARM TABLE UPGRADE START
		// *************************
		// 1. back up old version 
		String tmpTable = getAlarmTableName() + "_temp";				
		String qery = "ALTER TABLE " + getAlarmTableName() + " RENAME TO " + tmpTable;			
		database.execSQL(qery);
		
		// 2. drop table
		database.execSQL("DROP TABLE IF EXISTS " + getAlarmTableName());
		
		// 3. create table
		createAlarmTable(database, newVersion);
		
		// 4. restore old db
		insertAlarm(database, oldVersion, newVersion, tmpTable);
		
		// 5. drop old temp table
		database.execSQL("DROP TABLE IF EXISTS " + tmpTable);			
		// *************************
		// ALARM TABLE UPGRADE END
		// *************************
	}
		
		/**
		 * Method onUpgrade.
		 * @param db SQLiteDatabase
		 * @param oldVersion int
		 * @param newVersion int
		 */
		@Override
		public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
			
			Log.d(TAG, "Upgrade database ");
			Log.d(TAG, "old Version = " + oldVersion + " , new Version = " + newVersion);	

			onUpgradeAlarmTable(database, oldVersion, newVersion);
			//db.execSQL("DROP TABLE IF EXISTS calcus");
			
			onCreate(database);	
		} 		
		
		@Override
		public void onDowngrade(SQLiteDatabase database, int oldVersion,
				int newVersion) {
			// TODO Auto-generated method stub
//			super.onDowngrade(db, oldVersion, newVersion);
			Log.d(TAG, "onDowngrade database");
			Log.d(TAG,"old Version = " + oldVersion + " , new Version = " + newVersion);
			
			onUpgradeAlarmTable(database, oldVersion, newVersion);
			
//			db.execSQL("DROP TABLE IF EXISTS " + getDbName());
//			db.execSQL("DROP TABLE IF EXISTS calcus");
			
			onCreate(database);
		}
	}
	

		
	/**
	 * Method onCreate.
	 * @return boolean
	 */
	@Override
	public boolean onCreate() {
		
		mOpenHelper = new DatabaseHelper(getContext());
		
		return true;
	}
	 
	/**
	 * Method query.
	 * @param url Uri
	 * @param projection String[]
	 * @param selection String
	 * @param selectionArgs String[]
	 * @param sort String
	 * @return Cursor
	 */
	@Override
	public Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs, String sort) {
		SQLiteQueryBuilder qb=new SQLiteQueryBuilder();
		String table = "";
		String orderBy = null;
		db = mOpenHelper.getWritableDatabase();
		//String groupBy = null;
		
		switch(MATCHER.match(url)) {
			case ALARMS :
				String queryType = url.getQueryParameter("queryType");
				table =	getAlarmTableName();
				qb.setTables( table );   
				
				if(queryType!=null && queryType.equals("1")) {					
					if (sort != null && !"".equals(sort.trim()))
						orderBy= sort;    
				}else {
					qb.setProjectionMap(sAlarmsListProjection);
					orderBy= sort;
				}  
				break;
			
			case ALARM_ID :
				table =	getAlarmTableName();
				qb.setTables( table );
				qb.appendWhere(  "_id="+url.getPathSegments().get(1)  );	
				orderBy= sort;    	
				break;
				
			///////////////////////////////////////////////////////////////////////////////
			case CALCUS :
						 		
				String querycalcuType = url.getQueryParameter("calcuqueryType");				
				if(querycalcuType!=null && querycalcuType.equals("1")){
					table =	"calcus";  
					qb.setTables( table );   
					
					if (sort != null && !"".equals(sort.trim())) 
						orderBy= sort;   
					else   
						orderBy = "time, status, aid";
					
				} else if(querycalcuType!=null && querycalcuType.equals("2")){
					table =	"calcus";  
					qb.setTables( table );   
					//groupBy = "time, status, aid";
					if (sort != null && !"".equals(sort.trim())) 
						orderBy= sort;   
					else   
						orderBy = "time, status, aid";
					   
				} else {
					table =	"calcus";
					qb.setTables( table );
					qb.setProjectionMap(sCalcusListProjection);	
					if (sort != null && !"".equals(sort.trim())) 
						orderBy= sort;   
					else   
						orderBy = "time, status, aid";
				}
				break;	
			    
			case CALCU_ID :
				table =	"calcus";
				qb.setTables( table );
				qb.appendWhere(  "_id="+url.getPathSegments().get(1)  );
				if (sort != null && !"".equals(sort.trim())) 
					orderBy= sort;   
				else   
					orderBy = "time, status, aid";   
				break;
				
			///////////////////////////////////////////////////////////////////////////////
			
			default :
				break;
		} 
	    //////////////////////////////////////////////////////////////////////////////////////
		Log.d(TAG, "query -> PROJECTION : "+projection+", SELECTION : "+selection+", SELECTION ARGS : "+selectionArgs+", ORDER BY : "+orderBy);
		Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);	
		
		if(c != null){
			c.setNotificationUri(getContext().getContentResolver(), url);
			Log.d(TAG, "query -> setNotificationUri url : "+url);
		}
		
		
		return c;
	}
   	
	/**
	 * Method getType.
	 * @param url Uri
	 * @return String
	 */
	@Override 
	public String getType(Uri url) {
		
		if ( MATCHER.match(url)==ALARMS ) {
			return ("vnd.android.cursor.dir/"+getAlarmTableName());
		
		} else if ( MATCHER.match(url)==ALARM_ID ) {
		    return "vnd.android.cursor.item/"+getAlarmTableName() ;
		/////////////////////////////////////////////////////////////
		    
		} else if ( MATCHER.match(url)==CALCUS ) {
			return ("vnd.android.cursor.dir/calcus");
			
		} else if ( MATCHER.match(url)==CALCU_ID ) {
			return("vnd.android.cursor.item/calcus");
		////////////////////////////////////////////////////////////////
		}    
 
		return "vnd.android.cursor.item/cities" ;
	}

	  
	/**
	 * Method insert.
	 * @param url Uri
	 * @param values ContentValues
	 * @return Uri
	 */
	@Override
	public Uri insert(Uri url, ContentValues values) {
		db = mOpenHelper.getWritableDatabase();
		// Validate the requested uri
	    if (MATCHER.match(url) != ALARMS && MATCHER.match(url) != CALCUS
	    		&& MATCHER.match(url) != CITIES ) {
	    	IllegalArgumentException e = new IllegalArgumentException("Unknown URI " + url);
	    	
	    	Log.d(TAG, "insert -> IllegalArgumentException url : "+url);
	    	
	    	throw e;
	    }
	      
	    long id = 0;
	    Uri newUri = null;
	    // Insert into database]
	        
	    if (MATCHER.match(url) == ALARMS) {
	    	id = db.insertOrThrow(getAlarmTableName(), null, values);
	      		
	      	// Notify any watchers of the change
	    	newUri = ContentUris.withAppendedId(CONTENT_ALARMS_URI, id);
	      	getContext().getContentResolver().notifyChange(newUri, null);
	      	Log.d(TAG, "insert -> MATCHER.match(url) == ALARMS, url: "+newUri);
	      	return newUri;
	      		
	    } else if (MATCHER.match(url) == CALCUS) {
	    	id = db.insertOrThrow("calcus", null, values);
		    	 
	    	// Notify any watchers of the change
			newUri = ContentUris.withAppendedId(CONTENT_CALCU_URI, id);
			getContext().getContentResolver().notifyChange(newUri, null);
	     
			Log.d(TAG, "insert -> MATCHER.match(url) == CALCUS, url: "+newUri);
			return newUri;
			    
	    } 
	      
	    Log.d(TAG, "insert -> nothing , url: "+newUri);
	    return newUri;
	}
	
	/** Append an id test to a SQL selection expression */
	private String appendRowId(String where, long id) {
		return "_id=" + id  + (!TextUtils.isEmpty(where)  ? " AND (" + where + ')' : "") ;
	}

	   
	/**
	 * Method delete.
	 * @param url Uri
	 * @param where String
	 * @param whereArgs String[]
	 * @return int
	 */
	@Override
	public int delete(Uri url, String where,   String[] whereArgs) 
	{
		StringBuilder logs = new StringBuilder();
		logs.append("delete -> ");
		
		db = mOpenHelper.getWritableDatabase();
		int count = 0;
		switch (MATCHER.match(url)) {
	    	case ALARMS:
	    		count = db.delete(getAlarmTableName(), where, whereArgs);
	    		logs.append("ALARMS ");
	    		break;
		          
	    	case ALARM_ID:
	    		long id = Long.parseLong(url.getPathSegments().get(1));
	    		count = db.delete(getAlarmTableName(),  appendRowId(where, id),  whereArgs);
	    		logs.append("ALARM_ID ");
	    		break;
	         
	    	case CALCUS:
	    		count = db.delete("calcus", where, whereArgs);
	    		logs.append("CALCUS ");
	    		break;
	         
	    	case CALCU_ID:
	    		id = Long.parseLong(url.getPathSegments().get(1));
	    		count = db.delete("calcus",  appendRowId(where, id),  whereArgs);
	    		logs.append("CALCU_ID ");
	    		break;
	    		
	    	default:
	    		IllegalArgumentException e = new IllegalArgumentException("Unknown URI " + url);
	    		Log.d(TAG, logs.toString()+" IllegalArgumentException "+", URI : "+url+", where : "+where);
	    		throw e;
		}
	    
		Log.d(TAG, logs.toString()+", URI : "+url+", where : "+where+", whereArgs : "+whereArgs);
		getContext().getContentResolver().notifyChange(url, null);
		return count;
	}
      
	/**
	 * Method update.
	 * @param url Uri
	 * @param values ContentValues
	 * @param where String
	 * @param whereArgs String[]
	 * @return int
	 */
	@Override
	public int update(Uri url, ContentValues values, String where, String[] whereArgs) 
	{
		StringBuilder logs = new StringBuilder();
		logs.append("update -> ");
		
		db = mOpenHelper.getWritableDatabase();
		int count = 0;
		long rowId = 0;
		switch (MATCHER.match(url)) {
	      	case ALARMS:
	      		logs.append("ALARMS ");
	      		count = db.update(getAlarmTableName(), values, where,  whereArgs);
		        break;
	         
	      	case ALARM_ID:
	      		logs.append("ALARM_ID ");
	      		String segment = url.getPathSegments().get(1);
	            rowId = Long.parseLong(segment);
	            count = db.update(getAlarmTableName(), values, "_id=" + rowId, null);
	            break;
			         
	      	case CALCUS:
	      		logs.append("CALCUS ");
		    	count = db.update("calcus", values, where,  whereArgs);
			    break;
		         
		    case CALCU_ID:
		    	logs.append("CALCU_ID ");
	      		segment = url.getPathSegments().get(1);
                rowId = Long.parseLong(segment);
                count = db.update("calcus", values, "_id=" + rowId, null);
		        break;  
		         
	      	default:
	      		Log.d(TAG, logs.toString()+" IllegalArgumentException "+", URI : "+url+", where : "+where);
	      		IllegalArgumentException err = new IllegalArgumentException("Unknown URI " + url);
	      		throw err;
		}
    
		Log.d(TAG, logs.toString()+", URI : "+url+", where : "+where+", whereArgs : "+whereArgs);
		getContext().getContentResolver().notifyChange(url, null);
		return count;
	}	
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
}